package com.blb.demo4;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {

    public static final int PORT = 8888;

    public void start() throws IOException {


        try {
            String fileAddr = "C:\\Users\\廉壮\\IdeaProjects\\test\\_20230720_day04network\\src\\com\\blb\\demo4\\images2\\a.png";
            FileOutputStream fileOutputStream = new FileOutputStream(fileAddr);
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                Socket client = serverSocket.accept();
                InputStream inputStream = client.getInputStream();
                byte[] data = new byte[1024];
                while (true) {
                    int len = inputStream.read(data);
                    if (len == -1) {
                        break;
                    }
                    fileOutputStream.write(data);
                }

                System.out.println();

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }


    public static void main(String[] args) throws IOException {
        new FileServer().start();
    }


}
